﻿namespace HuaWei
{
    using ICSharpCode.SharpZipLib.Checksums;
    using ICSharpCode.SharpZipLib.Zip;
    using System;
    using System.Collections;
    using System.IO;
    using System.Runtime.InteropServices;

    public class FileZip
    {
        public static string unzipFile(string sourceFileName, string destPath, string pass = "", bool overwrite = true)
        {
            ZipInputStream zipInputStream = new ZipInputStream(File.OpenRead(sourceFileName));
            new ArrayList();
            string text = "";
            ZipEntry nextEntry;
            string result;
            if ((nextEntry = zipInputStream.GetNextEntry()) != null)
            {
                text = Path.GetFileName(nextEntry.Name);
                if (text != "")
                {
                    text = destPath + "\\" + text;
                    if (File.Exists(text))
                    {
                        if (!overwrite.Equals(true))
                        {
                            result = "";
                            return result;
                        }
                        File.Delete(text);
                    }
                    if (!Directory.Exists(destPath))
                    {
                        Directory.CreateDirectory(destPath);
                    }
                    FileStream fileStream = File.Create(text);
                    byte[] array = new byte[2048];
                    zipInputStream.Password = pass;
                    while (true)
                    {
                        int num = zipInputStream.Read(array, 0, array.Length);
                        if (num <= 0)
                        {
                            break;
                        }
                        fileStream.Write(array, 0, num);
                    }
                    fileStream.Close();
                }
            }
            zipInputStream.Close();
            result = text;
            return result;
        }

        public static void zipFile(string pathName, string sourceFileName, string destFileName, [Optional, DefaultParameterValue("")] string pass)
        {
            string path = pathName + destFileName;
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            Crc32 crc = new Crc32();
            ZipOutputStream stream = new ZipOutputStream(File.Create(path));
            stream.Password = pass;
            stream.SetLevel(6);
            FileStream stream2 = File.OpenRead(pathName + sourceFileName);
            byte[] buffer = new byte[stream2.Length];
            stream2.Read(buffer, 0, buffer.Length);
            ZipEntry entry = new ZipEntry(sourceFileName);
            entry.DateTime = DateTime.Now;
            entry.Size = stream2.Length;
            stream2.Close();
            crc.Reset();
            crc.Update(buffer);
            entry.Crc = crc.Value;
            stream.PutNextEntry(entry);
            stream.Write(buffer, 0, buffer.Length);
            stream.Finish();
            stream.Close();
        }

        public static void zipPath(string path, string destFileName)
        {
            Crc32 crc = new Crc32();
            ZipOutputStream stream = new ZipOutputStream(File.Create(destFileName));
            stream.Password = "";
            stream.SetLevel(6);
            DirectoryInfo info = new DirectoryInfo(path);
            if (info.Exists)
            {
                foreach (FileInfo info2 in info.GetFiles())
                {
                    FileStream stream2 = File.OpenRead(info2.FullName);
                    byte[] buffer = new byte[stream2.Length];
                    stream2.Read(buffer, 0, buffer.Length);
                    ZipEntry entry = new ZipEntry(info2.FullName);
                    entry.DateTime = DateTime.Now;
                    entry.Size = stream2.Length;
                    stream2.Close();
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    stream.PutNextEntry(entry);
                    stream.Write(buffer, 0, buffer.Length);
                }
                stream.Finish();
                stream.Close();
            }
        }
    }
}

